//
//  LLOrderView.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/7.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLOrderView.h"
#import "LLShopGoodsModel.h"

#define LLContentH 340
#define LLLeftMargin 5

@interface LLOrderView ()
@property (strong, nonatomic) LLButton *coverBtn;
@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UIImageView *imgView;
@property (strong, nonatomic) UILabel *titleLbl;
@property (strong, nonatomic) UILabel *unitPriceLbl;
@property (strong, nonatomic) LLButton *closeBtn;
@property (strong, nonatomic) LLButton *reduceBtn;
@property (strong, nonatomic) LLButton *addBtn;
@property (strong, nonatomic) UILabel *countLbl;
@property (strong, nonatomic) UILabel *totalPriceLbl;
@property (strong, nonatomic) LLButton *doBtn;
@end

@implementation LLOrderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    _coverBtn = [LLButton new];
    _coverBtn.backgroundColor = [UIColor blackColor];
    _coverBtn.alpha = 0.02;
    _coverBtn.frame = CGRectMake(0, 0, self.width, self.height);
    [_coverBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_coverBtn];
    
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, LLContentH)];
    _contentView.backgroundColor = UIColor.whiteColor;
    [self addSubview:_contentView];
    
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(LLLeftMargin, 8, 80, 80)];
    _imgView.backgroundColor = LLColorRGB(210);
    _imgView.contentMode = UIViewContentModeScaleAspectFill;
    _imgView.clipsToBounds = YES;
    _imgView.layer.cornerRadius = 5;
    [_contentView addSubview:_imgView];
    
    _closeBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"close" titleColor:nil font:0];
    _closeBtn.frame = CGRectMake(self.width-5-30, 0, 30, 30);
    [_closeBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_closeBtn];
    
    _titleLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(62) font:13 textAlignment:NSTextAlignmentLeft];
    _titleLbl.frame = CGRectMake(_imgView.right+LLLeftMargin, _imgView.top+5, self.width-_imgView.right-LLLeftMargin-40, 0);
    _titleLbl.numberOfLines = 3;
    [_contentView addSubview:_titleLbl];
    
    _unitPriceLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColor(231, 11, 11) font:20 textAlignment:NSTextAlignmentLeft];
    _unitPriceLbl.frame = CGRectMake(_titleLbl.left, _imgView.bottom-20, self.width-_titleLbl.left, 20);
    [_contentView addSubview:_unitPriceLbl];
    
    CALayer *separateLine = CALayer.layer;
    separateLine.frame = CGRectMake(LLLeftMargin, _imgView.bottom+8, self.width-2*LLLeftMargin, 0.7);
    separateLine.backgroundColor = LLColorRGB(191).CGColor;
    [_contentView.layer addSublayer:separateLine];
    
    UILabel *quantityLbl = [LLInitBaseViewTool labelWithText:@"数量" textColor:LLColorRGB(112) font:13 textAlignment:NSTextAlignmentLeft];
    quantityLbl.frame = CGRectMake(LLLeftMargin, separateLine.bottom, 40, 30);
    [_contentView addSubview:quantityLbl];
    
    UIView *doQuantityView = [[UIView alloc] initWithFrame:CGRectMake(LLLeftMargin, quantityLbl.bottom, 150, 37)];
    doQuantityView.layer.borderColor = LLColorRGB(160).CGColor;
    doQuantityView.layer.borderWidth = 1;
    doQuantityView.layer.cornerRadius = 3;
    doQuantityView.clipsToBounds = YES;
    [_contentView addSubview:doQuantityView];
    
    _reduceBtn = [LLInitBaseViewTool buttonWithTitle:@"－" image:nil titleColor:LLColorRGB(160) font:0];
    _reduceBtn.frame = CGRectMake(0, 0, doQuantityView.height, doQuantityView.height);
    _reduceBtn.tag = 0;
    _reduceBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [doQuantityView addSubview:_reduceBtn];
    [_reduceBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    CALayer *verticalLine1 = CALayer.layer;
    verticalLine1.frame = CGRectMake(_reduceBtn.right, 0, 1, doQuantityView.height);
    verticalLine1.backgroundColor = LLColorRGB(160).CGColor;
    [doQuantityView.layer addSublayer:verticalLine1];
    
    _countLbl = [LLInitBaseViewTool labelWithText:@"1" textColor:LLColorRGB(0) font:20 textAlignment:NSTextAlignmentCenter];
    _countLbl.frame = CGRectMake(verticalLine1.right, 0, doQuantityView.width-doQuantityView.height*2-2, doQuantityView.height);
    [doQuantityView addSubview:_countLbl];
    
    CALayer *verticalLine2 = CALayer.layer;
    verticalLine2.frame = CGRectMake(_countLbl.right, 0, 1, doQuantityView.height);
    verticalLine2.backgroundColor = LLColorRGB(160).CGColor;
    [doQuantityView.layer addSublayer:verticalLine2];
    
    _addBtn = [LLInitBaseViewTool buttonWithTitle:@"＋" image:nil titleColor:LLColorRGB(160) font:0];
    _addBtn.frame = CGRectMake(verticalLine2.right, 0, doQuantityView.height, doQuantityView.height);
    _addBtn.tag = 1;
    _addBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [doQuantityView addSubview:_addBtn];
    [_addBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    CALayer *separateLine1 = CALayer.layer;
    separateLine1.frame = CGRectMake(LLLeftMargin, doQuantityView.bottom+8, self.width-2*LLLeftMargin, 0.7);
    separateLine1.backgroundColor = LLColorRGB(191).CGColor;
    [_contentView.layer addSublayer:separateLine1];
    
    _totalPriceLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(112) font:13 textAlignment:NSTextAlignmentLeft];
    _totalPriceLbl.frame = CGRectMake(LLLeftMargin, separateLine1.bottom, self.width-LLLeftMargin*2, 40);
    [_contentView addSubview:_totalPriceLbl];
    
    CALayer *separateLine2 = CALayer.layer;
    separateLine2.frame = CGRectMake(LLLeftMargin, _totalPriceLbl.bottom, self.width-2*LLLeftMargin, 0.7);
    separateLine2.backgroundColor = LLColorRGB(191).CGColor;
    [_contentView.layer addSublayer:separateLine2];
    
    _doBtn = [LLInitBaseViewTool buttonWithTitle:@"加入购物车" image:nil titleColor:LLColorRGB(255) font:17];
    _doBtn.backgroundColor = LLColor(225, 0, 0);
    _doBtn.frame = CGRectMake(0, separateLine2.bottom + 30, self.width, 50);
    _doBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [_contentView addSubview:_doBtn];
    [_doBtn addTarget:self action:@selector(doBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setModel:(LLShopGoodsModel *)model {
    _model = model;
    [self.imgView sd_setImageWithURL:LLImageWithUrl(model.goods_pic) placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRefreshCached];
    CGFloat titleH = [CommonTools sizeOfText:model.goods_name fontSize:13 width:self.width-_imgView.right-LLLeftMargin-40].height;
    titleH = titleH > _imgView.height-_unitPriceLbl.height-5 ? 56 : titleH;
    self.titleLbl.height = titleH;
    self.titleLbl.text = model.goods_name;
    self.unitPriceLbl.text = model.goods_point;
    CGFloat totalPrice = model.goods_buy.floatValue * self.countLbl.text.integerValue;
    self.totalPriceLbl.text = [NSString stringWithFormat:@"共计：¥%g", totalPrice];
}

- (void)btnClick:(LLButton *)btn {
    NSInteger count = self.countLbl.text.integerValue;
    if (btn.tag == 0) {
        if (count >= 2) {
            count--;
        }
    } else {
        count++;
    }
    self.countLbl.text = [NSString stringWithFormat:@"%ld",count];
    CGFloat totalPrice = self.model.goods_point.floatValue * count;
    self.totalPriceLbl.text = [NSString stringWithFormat:@"共计：¥%g",totalPrice];
}

- (void)doBtnClick {
    
}

// 显示popView
- (void)show {
    self.hidden = NO;
    [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        self.contentView.top = self.height - LLContentH;
    } completion:^(BOOL finished) {
    }];
}

// 隐藏popView
- (void)hide {
    [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        self.contentView.top = self.height;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

@end
